package old;

import utils.IntStrPair;

import java.io.*;
import java.math.*;
import java.util.*;

public class OldVocabulary {
    
    // N noun
    // V verb
    // A adjective
    // C conjunction (and, but, or, yet)
    // P preposition (in, on, since, towards, until)
    // D definite article
    // I indefinite article
    // ! Interjection (yeah, pss, phew)
    public static final String PART_STR = "NVACPDI!";
    public static final int PN = PART_STR.length();


    private boolean wordCharacter(char c) {
        return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z'/* || c == '\''*/;
    }

    private List<String> parseWordsFromFile(String filename) throws IOException {
System.err.println(filename);
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(filename));
        } catch (Exception e) {
            return new ArrayList<String>();
        }
        StringBuilder sb = new StringBuilder();
        String tmp;
        while ((tmp = br.readLine()) != null) {
            sb.append(tmp).append("\n");
        }
        br.close();
        char[] c = sb.toString().toCharArray();
        int n = c.length;
        List<String> words = new ArrayList<String>();
        for (int i = 0; i < n;) {
            while (i < n && !wordCharacter(c[i])) i++;
            if (i == n) break;
            int j = i;
            while (j < n && wordCharacter(c[j])) j++;
            words.add(sb.substring(i, j).toLowerCase());
            i = j;
        }
        return words;
    }

    private Map<String, List<String>> makeNeighbourWordsMap(List<String> filenames) throws IOException {
        Map<String, List<String>> m = new HashMap<String, List<String>>();
        for (String filename : filenames) {
            if (filename.length() == 0) continue;
            List<String> words = parseWordsFromFile(filename);
            int n = words.size();
            for (int i = 0; i < n - 1; i++) {
                String word = words.get(i);
                String nextWord = words.get(i + 1);
                List<String> list = m.get(word);
                if (list == null) {
                    list = new ArrayList<String>();
                    m.put(word, list);
                }
                list.add(nextWord);
            }
        }
        return m;
    }

    private Map<String, List<IntStrPair>> makeOccurenceMap(Map<String, List<String>> m) {
        Map<String, List<IntStrPair>> result = new TreeMap<String, List<IntStrPair>>();
        for (String word : m.keySet()) {
            List<String> nexts = m.get(word);
            Collections.sort(nexts);
            List<IntStrPair> pairs = new ArrayList<IntStrPair>();
            int n = nexts.size();
            for (int i = 0; i < n;) {
                int j = i;
                while (j < n && nexts.get(j).equals(nexts.get(i))) j++;
                pairs.add(new IntStrPair(j - i, nexts.get(i)));
                i = j;
            }
            Collections.sort(pairs);
            result.put(word, pairs);
        }
        for (String word : result.keySet()) {
            Collections.sort(result.get(word));
        }
        return result;
    }


    public static Map<String, List<IntStrPair>> parseResult(String dbFilename) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(dbFilename));
        String tmp;
        Map<String, List<IntStrPair>> result = new HashMap<String, List<IntStrPair>>();
        while ((tmp = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(tmp);
            String word = st.nextToken();
            List<IntStrPair> l = new ArrayList<IntStrPair>();
            result.put(word, l);
            while (st.hasMoreTokens()) {
                int x = Integer.parseInt(st.nextToken());
                String s = st.nextToken();
                l.add(new IntStrPair(x, s));
            }
        }
        br.close();
        return result;
    }

    
    public static Map<String, Integer> parsePartsOfSpeech(String partOfSpeechFile, Map<String, List<IntStrPair>> result) throws IOException {
        Map<String, Integer> part = new HashMap<String, Integer>();
        BufferedReader br = new BufferedReader(new FileReader(partOfSpeechFile));
        String tmp;
        while ((tmp = br.readLine()) != null) {
            int x = tmp.lastIndexOf('\t');
            String word = tmp.substring(0, x).trim();
            if (word.indexOf(' ') >= 0) continue;
            word = word.toLowerCase();
            if (!result.containsKey(word)) continue;
            if (tmp.charAt(x + 1) == '|') x++;
            String p = tmp.substring(x + 1).trim();
            if (p.length() > 1) continue;
            int y = PART_STR.indexOf(p.charAt(0));
            if (y < 0) continue;
            part.put(word, y);
        }
        br.close();
        return part;
    }

}
